/*
 * @Author: Dannycjh 2233879049@qq.com
 * @Date: 2023-04-14 20:29:48
 * @LastEditors: Dannycjh 2233879049@qq.com
 * @LastEditTime: 2023-04-27 16:20:02
 * @FilePath: \cc-game-development-template\assets\scripts\App.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { game, sys } from "cc";
import EventManager from "./tools/EventManager";
import { LanguageMgr } from "./language/LanguageMgr";
import { PoolManager } from "./tools/PoolManager";
import SceneNavigator from "./tools/SceneNavigator";
import { onClick } from "./tools/OnClick";
import { localKey, Singleton } from "./common/Common";
import UtilTool from "./tools/UtilTool";
import assetsMgr from "./tools/AssetsMgr";
import {AudioMgr} from "./tools/AudioMgr";
import { GlobalConfig } from "./common/GlobalConfig";
import { Model } from "./game/Model";
import { LoadMg, loadMg } from "./tools/LoadMg";
import { SpriteFrame } from "cc";
import { Hall } from "./hall/Hall";

class app extends Singleton<app>{
    private _config: any = null;
    private _localData: any = {};
    private _isSingle: boolean = true; // 是否单场景，即项目中有且仅有一个 scene 文件
    // 游戏类型
    public fightMode = 0
    fightMode_no = -1	// 无
	fightMode_1 = 1		// 幸运台球
    fightMode_4 = 2		// 血流到底
	fightMode_2 = 3		// 练习场
	fightMode_3 = 4		// 多人对战
	

    // 游戏状态
	WSGameStatus = 1		//  0：不在游戏中 1:幸运台球游玩中 2:多人模式游玩中 3:四球开伦游玩中(用于断线重连传参) 4:血流到底

    public isToHallFromMode1 = false    // 是否是从幸运台球游戏返回到大厅
    public isShowGuide = false          // 是否展示过新手引导

    public SHOW_LOG = true          // 是否展示日志
    public SHOW_Link = 1            // 环境链接 [0]本地环境 [1]测试环境 [2]正式环境
    public EFFECT_SW = "1"          // 音效开关 [0]关闭 [1]打开
    public MUSIC_SW = "1"           // 音乐开关 [0]关闭 [1]打开

    public hrefLinkIp = null    // 访问地址IP参数
    public hrefLinkWs = null    // 访问地址长链接协议参数
    public textareaInput = null // 剪切板对象

    public publicKey = ''       // 加密的秘钥
    public isEncrypt = false    // 发送消息是否加密

    public hallPanel:Hall = null // 大厅脚本界面

    public HALL_RES_LIST = [
        {path: "images/head", type: SpriteFrame, isDir : true},
        // {path: "prefabs/game/comboNode", type: Prefab, isDir : false},
    ]

    public get Event(): EventManager { return EventManager.getInstance<EventManager>(EventManager) };
    public get Asset(): assetsMgr { return assetsMgr.getInstance<assetsMgr>(assetsMgr); }
    public get Util(): UtilTool { return UtilTool.getInstance<UtilTool>(UtilTool); }
    public get Scene(): SceneNavigator { return SceneNavigator.getInstance<SceneNavigator>(SceneNavigator) }
    public get PoolManager(): PoolManager { return PoolManager.getInstance<PoolManager>(PoolManager) }
    public get onClick(): onClick { return onClick.getInstance<onClick>(onClick) }
    public get language(): LanguageMgr { return LanguageMgr.getInstance<LanguageMgr>(LanguageMgr) }
    public get Model(): Model { return Model.getInstance<Model>(Model); }
    public get Audio(): AudioMgr { return AudioMgr.getInstance<AudioMgr>(AudioMgr); }
    public get GlobalConfig(): GlobalConfig { return GlobalConfig.getInstance<GlobalConfig>(GlobalConfig); }
    public get LoadMg(): loadMg { return LoadMg }

    public init() {
        this.EFFECT_SW = App.Util.getLocalItem(localKey.EFFECT_SW, "1", false);
        this.MUSIC_SW = App.Util.getLocalItem(localKey.MUSIC_SW, "1", false);
    }
    
    /**
     * 设置音效开关
     * @param v 值
     */
    public setEffect(v){
        this.EFFECT_SW = v?"1":"0"

        App.Util.setLocalItem(localKey.EFFECT_SW, this.EFFECT_SW);
    }

    /**
     * 设置音乐开关
     * @param v 值
     */
    public setMusic(v){
        this.MUSIC_SW = v?"1":"0"
        App.Util.setLocalItem(localKey.MUSIC_SW, this.MUSIC_SW);

        if (v) {
            if (App.Scene.curScene == "home") {
                App.Audio.play("home")
            }
            else if (App.Scene.curScene == "game1" || App.Scene.curScene == "game2" || App.Scene.curScene == "game3" || App.Scene.curScene == "game4") {
                App.Audio.play("game")
            }
        }
        else{
            App.Audio.stop()
        }
    }

    /**
     * 获取本地数据
     * @param key 键
     */
    public setLocalStorage(key: string, data: any) {
        if (!key || key.length < 0)
            this._localData = data;
        else this._localData[key] = data;
        let _str = JSON.stringify(this._localData);//this.String.encode64(this._localData);
        sys.localStorage.setItem(this.getValue('project'), _str);
    }
    /**
     * 获取本地数据
     * @param key 键
     */
    public getLocalStorage(key: string, def?: any): any {
        const data: any = !this.Util.isEmpty(key) && this._localData[key];
        this.Util.isInvalid(data) && !this.Util.isInvalid(def) && this.setLocalStorage(key, def);
        return data || def;
    }
    /**
     * 移除本地数据
     * @param key 键
     */
    public removeLocalStorage(key: string) {
        sys.localStorage.removeItem(key);
    }

    public getValue(key: string): any {
        if (this.Util.isEmpty(key)) return this._config;
        let _paths = key.split('/');
        let _val: any = this._config[_paths.shift()];
        while (_paths.length > 0) {
            if (!_val) return _val;
            _val = _val[_paths.shift()];
        }
        if (this.Util.isInvalid(_val)) {
            const not = `WARN:App.getCfg(${key}) is invalid.`;
            console.warn(not);
        }
        return _val;
    }
}

export const App: app = app.getInstance<app>(app);

